package MTT::Logger;

use strict;
use warnings;
use utf8;

use Log::Log4perl;

my ($user_log, $alarm_log);


sub setup {
    my $app = shift;    
    $app->maybe::next::method(@_);

	Log::Log4perl->init( $app->path_to('logger.conf')->stringify() );

	$app->config->{'Plugin::Logger'} ||= {};
	my $conf = $app->config->{'Plugin::Logger'};

	my $user_log_name = $conf->{'user_log'}  || 'All'; 
	my $alarm_log_name = $conf->{'alarm_log'}  || 'Alarm'; 

	$user_log  = Log::Log4perl->get_logger( $user_log_name );
	$alarm_log = Log::Log4perl->get_logger( $alarm_log_name);

	unless ($user_log) {
		$app->log->error('error loading user log');
	}
	else {
    	$app->log->debug('user log loaded');
	}

	unless ($alarm_log) {
		$app->log->error('erroir loading alarm log');
	}
	else {
    	$app->log->debug('alarm log loaded');
	}

	foreach ( keys %{ Log::Log4perl->appenders() } ) {
		my $appender = Log::Log4perl->appender_by_name($_);
		if ( exists $appender->{dbh} && $appender->{dbh} ) {
			$appender->{dbh}->do('SET NAMES utf8');
			$appender->{dbh}->{mysql_enable_utf8} = 1;
		}
	}

    return $app;
}

sub user_log() {
    return $user_log;
}

sub alarm_log() {
	return $alarm_log;
}

1;
